//Start: 2024-12-26 10:00:27
#include<bits/stdc++.h>
#define For(i,j,k) for(int i=(j);i<=(k);++i)
#define ForDown(i,j,k) for(int i=(j);i>=(k);--i)
#define Debug(fmt, args...) fprintf(stderr,"(func %s, line #%d): " fmt, __func__, __LINE__, ##args),fflush(stderr)
#define debug(fmt, args...) fprintf(stderr,fmt,##args),fflush(stderr)
#define within :
#define LJY main
using namespace std;
typedef long long ll;
const int N=5e5+5;
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
char buf[N<<2],*p1,*p2;
#define getchar() ((p1==p2&&(p2=(p1=buf)+fread(buf,1,N<<2,stdin),p1==p2))?0:*p1++)
inline int read(){
  char ch=getchar();int x=0,f=1;
  while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
  while(ch>='0'&&ch<='9')
    x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
  return x*f;
}
int n;
vector<int>vec[N];
vector<int>lis;int len;
vector<pair<int,int> >lp;
ll b[N],c;int cnt[N<<1];
int gval(int x){return lower_bound(lis.begin(),lis.end(),x)-lis.begin()+1;}
ll Mn;vector<int>ans;
signed LJY(){
  freopen("list.in","r",stdin);
  freopen("list.out","w",stdout);
  n=read();
  ll cnt1=0,res1=0,res2=0;
  lis.reserve(n);lp.reserve(n);
  For(i,1,n){
    int k=read();cnt1+=k;vec[i].resize(k);
    For(j,0,k-1) vec[i][j]=read(),lis.emplace_back(vec[i][j]);
  }c=read();
  For(i,1,n) b[i]=(ll)read()*i;
  sort(lis.begin(),lis.end());lis.erase(unique(lis.begin(),lis.end()),lis.end());len=lis.size();
  For(i,1,n) for(int x within vec[i])
    cnt[gval(x)]++;
  For(i,1,len) res2+=b[cnt[i]];
  For(i,1,n){
    int k=vec[i].size();
    For(j,1,k-1) lp.emplace_back(vec[i][j]-vec[i][j-1],gval(vec[i][j]));
  }sort(lp.begin(),lp.end());
  Mn=(cnt1+res1)*c+res2;ans.emplace_back(1);
  int ln=lp.size();
  For(i,0,ln-1){
    int j=i;
    while(j<ln&&lp[j].first==lp[i].first){
      res2-=b[cnt[lp[j].second]];
      cnt[lp[j].second]--;
      res2+=b[cnt[lp[j].second]];
      res1+=lp[j].first;cnt1--;
      j++;
    }ll val=((cnt1*(lp[i].first+1)+res1)*c+res2);
    if(val==Mn) ans.emplace_back(lp[i].first+1);
    else if(val<Mn) Mn=val,ans.clear(),ans.emplace_back(lp[i].first+1);
    i=j-1;
  }printf("%lld %d\n",Mn,ans.size());
  for(int x within ans) printf("%d ",x);
  return 0;
}